<?php

use frontend\modules\common\models\BaseParam;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\widgets\LinkPager;
use yii\helpers\Html;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\resource\models\HySupportSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = $title;
$this->params['breadcrumbs'][] = $title;
?>
    <div class="hy-support-index">

        <?php Pjax::begin(['id' => 'centerView']); ?>
        <div class="searchBody">
            <?= $this->render('_search', ['model' => $searchModel]); ?>

        </div>
        <div>
            <?= Html::button('批量取消推荐', ['class' => 'btn btn-danger z-remove']) ?>
        </div>

        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'columns' => [
                [
                    'class' => 'yii\grid\CheckboxColumn',
                    'name' => 'id',
                ],
                'res_id',
                'res_type' => [
                    'attribute' => 'res_type',
                    'value' => function ($model) {
                        return BaseParam::CodeHelper(BaseParam::$resourceType, $model->res_type);
                    }
                ],
                'res_title',
                //'br_id',
                'created_at',
                //'updated_at',
                //'deleted_at',
            ],
            'pager' => [
                'class' => LinkPager::className(),
                'firstPageLabel' => '首页',
                'prevPageLabel' => '《',
                'nextPageLabel' => '》',
                'lastPageLabel' => '尾页',
                //            'goPageLabel' => true,
                //            'totalPageLable' => '共x页',
                //            'goButtonLable' => 'GO',
                'maxButtonCount' => 5,
                'hideOnSinglePage' => false,
            ]
        ]); ?>
        <?php Pjax::end(); ?>
    </div>
    <script>
        <?php $this->beginBlock('js_block') ?>
        //批量删除
        $(document).on('click', '.z-remove', function () {

            let id = allChecked();
            if (id) {
                layer.confirm('确认取消推荐资源？', {icon: 3, title: ''}, function (index) {
                    $.ajax({
                        url: '<?=Url::toRoute('batch-remove')?>',//'/resource/resource/upload-file'
                        type: 'POST',
                        data: {
                            id: JSON.stringify(id),
                        },
                        dataType: "json",
                        success: function (msg) {

                            if (msg.status == 200) {
                                layer.msg(msg['msg'], {icon: 1, time: 1000});
                            } else {
                                layer.msg(msg['msg'], {icon: 2, time: 1000});
                            }
                            $.pjax.reload({container: "#centerView"});
                        },
                        error: function () {
                        }
                    });
                    layer.close(index);
                });
            } else {
                layer.msg('先选择记录');
            }
        });

        //获取复选框id
        function allChecked() {

            var id = [];
            var dom = $('.grid-view tbody').find('input[type=checkbox]:checked');
            if (dom.length) {
                $.each(dom, function () {
                    id.push($(this).val())
                });
            } else {
                return 0;
            }
            return id;
        }
        <?php $this->endBlock() ?>
    </script>
<?php $this->registerJs($this->blocks['js_block'], \yii\web\View::POS_END); ?>